﻿using System;
using System.Collections.Generic;
using System.IO;
using Docentric.Examples.Data.BusinessObjects;
using Docentric.Examples.Data.BusinessObjects.DataModel;
using Docentric.Word;
using System.Xml.Linq;


namespace Docentric.Examples.Browser
{
	public class SimpleValuesXmlExample : ExampleBase
	{
		// Constructor
		public SimpleValuesXmlExample()
		{
			Name = "SimpleValuesXml";
			Title = "Simple Values (Xml)";
			Description = "In this report, we want to generate a document displaying data of a customer (customer’s name, company, job title, address etc.). The template has one (default) data source representing an XML document containing customer data.";
		}


		// GenerateReport
		public override string GenerateReport()
		{
			// Load the customer XML.
			XElement customerXElement = XElement.Load(ApplicationPath + "\\Xml\\Customer.xml");

			// Create a temporary file for the generated report document.
			string reportDocumentFileName = System.IO.Path.GetTempPath() + "GeneratedReport_" + Guid.NewGuid().ToString() + ".docx";
			using (Stream reportDocumentStream = File.Create(reportDocumentFileName))
			{
				// Open the report template file.
				using (Stream reportTemplateStream = GetReportTemplate())
				{
					// Generate the report document using 'DocumentGenerator'.
					DocumentGenerator dg = new DocumentGenerator(customerXElement);
					DocumentGenerationResult result = dg.GenerateDocument(reportTemplateStream, reportDocumentStream);
					if (result.HasErrors)
					{
						foreach (Word.Error error in result.Errors) Console.Out.WriteLine(error.Message);
					}
				}
			}

			return reportDocumentFileName;
		}
	}
}
